<template>
  <div class="exam-list">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>招考信息列表</span>
          <el-button type="primary" @click="handleAdd">新增招考信息</el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="标题">
          <el-input v-model="searchForm.title" placeholder="请输入标题" clearable />
        </el-form-item>
        <el-form-item label="招考类型">
          <el-select v-model="searchForm.type" placeholder="请选择招考类型" clearable>
            <el-option label="公务员" value="公务员" />
            <el-option label="事业单位" value="事业单位" />
            <el-option label="教师招聘" value="教师招聘" />
          </el-select>
        </el-form-item>
        <el-form-item label="地区">
          <el-cascader
            v-model="searchForm.region"
            :options="regionOptions"
            placeholder="请选择地区"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格区域 -->
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
        @sort-change="handleSortChange"
      >
        <el-table-column prop="title" label="标题" min-width="200" show-overflow-tooltip />
        <el-table-column prop="type" label="招考类型" width="120" />
        <el-table-column label="地区" width="180">
          <template #default="{ row }">
            {{ row.province }}{{ row.city }}{{ row.district }}
          </template>
        </el-table-column>
        <el-table-column prop="publishTime" label="发布时间" width="180" sortable="custom">
          <template #default="{ row }">
            {{ formatDate(row.publishTime) }}
          </template>
        </el-table-column>
        <el-table-column label="报名时间" width="280">
          <template #default="{ row }">
            {{ formatDate(row.signupStartTime) }} 至 {{ formatDate(row.signupEndTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="reshu" label="招录人数" width="100" />
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.isShow ? 'success' : 'info'">
              {{ row.isShow ? '展示' : '不展示' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="数据统计" width="200">
          <template #default="{ row }">
            <el-tooltip content="浏览量">
              <el-tag size="small" class="mx-1">{{ row.viewCount }}</el-tag>
            </el-tooltip>
            <el-tooltip content="收藏量">
              <el-tag size="small" type="success" class="mx-1">{{ row.collectCount }}</el-tag>
            </el-tooltip>
            <el-tooltip content="分享量">
              <el-tag size="small" type="warning" class="mx-1">{{ row.shareCount }}</el-tag>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button-group>
              <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
              <el-button type="primary" link @click="handleView(row)">查看</el-button>
              <el-button type="danger" link @click="handleDelete(row)">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance } from 'element-plus'
import dayjs from 'dayjs'

// 表格数据
const tableData = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 搜索表单
const searchForm = reactive({
  title: '',
  type: '',
  region: []
})

// 地区选项
const regionOptions = ref([
  // 这里需要根据实际情况添加地区数据
])

// 格式化日期
const formatDate = (date: string | Date) => {
  if (!date) return '-'
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}

// 获取表格数据
const getTableData = async () => {
  loading.value = true
  try {
    // 这里替换为实际的API调用
    // const res = await getExamList({
    //   page: currentPage.value,
    //   pageSize: pageSize.value,
    //   ...searchForm
    // })
    // tableData.value = res.data.list
    // total.value = res.data.total
  } catch (error) {
    console.error('获取数据失败:', error)
    ElMessage.error('获取数据失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getTableData()
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

// 排序变化
const handleSortChange = ({ prop, order }) => {
  // 处理排序逻辑
}

// 分页大小变化
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableData()
}

// 页码变化
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableData()
}

// 新增
const handleAdd = () => {
  // 处理新增逻辑
}

// 编辑
const handleEdit = (row: any) => {
  // 处理编辑逻辑
}

// 查看
const handleView = (row: any) => {
  // 处理查看逻辑
}

// 删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm('确认删除该招考信息吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      // 这里替换为实际的删除API调用
      // await deleteExam(row.id)
      ElMessage.success('删除成功')
      getTableData()
    } catch (error) {
      console.error('删除失败:', error)
      ElMessage.error('删除失败')
    }
  })
}

onMounted(() => {
  getTableData()
})
</script>

<style lang="scss" scoped>
.exam-list {
  padding: 20px;

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .search-form {
    margin-bottom: 20px;
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }

  .mx-1 {
    margin: 0 4px;
  }
}
</style> 